<?php

/*
 * File: login.php
 * Descrizione: Pagina di login.
 * Debug: OK.
 * Optimize: OK.
*/

// Avvia una nuova sessione o riprende una sessione gi� creata
session_start();

// Include i file necessari
include('inc/config.inc.php');
include('inc/template.inc.php');
include('inc/db.inc.php');
include('inc/check.inc.php');

// Variabili
$aid = $_SESSION['aid'];
$anm = $_SESSION['anm'];

$content	= '';
$count		= 0;
$msg_box	= '';

// Cambio password
if ($_SESSION['changepass'] == 1)
{
	$changepass = '<div class="alert alert-warning">Sono passati pi&ugrave; di 6 mesi dall\'ultimo cambio password, cambiala adesso!</div>';
}

$query = "SELECT user_id, user_name, user_surname, user_image, user_level, user_guild, user_guildrank, user_sex, user_race FROM users WHERE account_id = '$aid'";
if ($results = $db->query($query)){
	while($row = $results->fetch_assoc())
	{
		// Variabili
		$user_guildrank = $row['user_guildrank'];
		$user_guild = $row['user_guild'];
		$user_race = $row['user_race'];
		// Dati della gilda
		$query1 = "SELECT guildranks.rank_name, guildranks.rank_image, guilds.guild_name FROM guildranks, guilds WHERE guildranks.rank_id = '$user_guildrank' AND guilds.guild_id = '$user_guild'";
		if ($results1 = $db->query($query1)){
				
			$row1 = $results1->fetch_assoc();
			
			$guild = ( ! empty($row1['guild_name']) AND $row1['guild_name'] == '') ? trim(htmlentities($row1['guild_name'])) : 'Nessuna';
			$rank = '<span class="glyphicon glyphicon-chevron-right"></span> <strong>Rango:</strong> ' . $row1['rank_name'] . ' <img src="img/guilds/' . $row1['rank_image'] . '" width="20px" height="20px" alt="' . $row1['rank_name'] . '" />';
			$guild_data = "<strong>Gilda: </strong>" . $guild;
			$guild_data .= ($guild != 'Nessuna') ? $rank : '';
			$results1 -> close();
		}
		// Dati della razza
		$query2 = "SELECT race_name, race_image FROM races WHERE race_id = '$user_race'";
		if($results2 = $db->query($query2)) {
			$row2 = $results2->fetch_assoc();
			$race_data = '<strong>Razza: </strong>'.$row2['race_name'].'<img src="img/races/'.$row2['race_image'].'" width="20px" height="20px" alt="'.$row2['race_name'].'"/>';
			$results2->close();
		}
		// Recupera il sesso del personaggio
		if($row['user_sex'] == 0) {
			$sex = 'Maschio';
		} else {
			$sex = 'Femmina';
		}
		
		$content .= '<div class="col-md-3"><div class="panel panel-primary">';
		$content .= '<div class="panel-heading">'.$row['user_name'].' '.$row['user_surname'].'</div>';
		$content .= '<div class="panel-body"><div class="thumbnail"><img src="' . $row['user_image'] . '" width="150px" height="150px" border="1px" alt="profileimage" /></div>';
		$content .= '<hr /><ul class="list-group"><li class="list-group-item"><strong>Sesso: </strong>' . $sex . '</li>';
		$content .= '<li class="list-group-item">' . $race_data . '</li>';
		$content .= '<li class="list-group-item">' . $guild_data . '</li></ul><hr />';
		$content .= '<form action="userlogin.php" method="post"><div class="text-center"><button type="submit" class="btn btn-primary">Seleziona</button><input type="hidden" value="'.$row['user_id'].'" name="user_id" /></div></form>';
		$content .= '</div></div></div>';
		$count = $count++;
	}
	
	// Creazione nuovo personaggio.
	$races = '';
	$query3 = "SELECT race_id, race_name FROM races WHERE race_subscribe = '1'";
	if($results3 = $db->query($query3)) {
		while($row3 = $results3->fetch_assoc()){
		$races .= '<option value="'.$row3['race_id'].'">'.$row3['race_name'].'</option>';
		}
	}
	// Se i personaggi attuali sono minori del massimo di personaggi concessi, mostra il form per creare un nuovo pg
	if ($count < $g_maxpg)
	{
		$content .= '<div class="col-md-3"><div class="panel panel-primary">';
		$content .= '<div class="panel-heading">Nuovo Personaggio</div>';
		$content .= '<div class="panel-body"><form action="create.php" method="post" role="form">';
		$content .= '<div class="form-group"><label for="name">Nome:</label> <input class="form-control" type="text" name="name" id="name" value="" /></div>';
		$content .= '<div class="form-group"><label for="surname">Cognome:</label> <input class="form-control" type="text" name="surname" id="surname" /></div>';
		$content .= '<div class="form-group"><label for="race">Razza:</label><select class="form-control" name="race" id="race">';
		$content .= $races;
		$content .= '</select></div>';
		$content .= '<div class="radio"><label for="sexm"><input type="radio" name="sex" id="sexm" value="0" checked="checked" />Maschio</label></div>';
		$content .= '<div class="radio"><label for="sexf"><input type="radio" name="sex" id="sexf" value="1"/>Femmina</label></div>';
		$content .= '<input type="hidden" name="account" value="'.$aid.'" /><hr />';
		$content .= '<button type="submit" class="btn btn-success">Crea Personaggio</button>';
		$content .= '</form></div>';
		$content .= '</div></div>';
	}
}
// Controlla gli esiti delle azioni
$mod = ( ! empty($_GET['mod'])) ? trim(mysql_real_escape_string($_GET['mod'])) : '';

	switch ($mod){
	// Creazione personaggio
	case 1:
		$msg_box .= '<div class="alert alert-success" align="left">';
		$msg_box .= 'Personaggio creato con successo.';
		$msg_box .= '</div>';
	break;
	case 2:
		$msg_box .= '<div class="alert alert-warning" align="left">';
		$msg_box .= 'Riempire tutti i campi per creare il personaggio.';
		$msg_box .= '</div>';
	break;
	// Modifica password
	case 3:
		$msg_box .= '<div class="alert alert-success" align="left">';
		$msg_box .= 'Password modificata con successo.';
		$msg_box .= '</div>';
	break;
	case 4:
		$msg_box .= '<div class="alert alert-warning" align="left">';
		$msg_box .= 'Password non modificata.';
		$msg_box .= '</div>';
	break;
}


// Lista delle tag speciali
$tags = array('temp_title' => $g_name, 'temp_content' => $content, 'temp_name' => $anm, 'temp_changepass' => $changepass, 'temp_msgbox' => $msg_box);

// Carica il template e formatta le tag speciali
$temp = new template('style/userselect.template.html');
$temp->replace($tags);

// Visualizza l'output
echo $temp->show();

?>